﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LevenBlog.Core.Data;
using LevenBlog.Core.Entities;
using LevenBlog.Core.Utils;

namespace LevenBlog.Core.Service
{
    public static class BlogUser
    {
        public static int Count(IDbProvider provider)
        {
            return ProviderManager.Factory.User.Count(provider);
        }

        public static int Count(IDbProvider provider, int typeid)
        {
            return ProviderManager.Factory.User.CountForTypeID(provider, typeid);
        }

        public static int Count(IDbProvider provider, string searchName)
        {
            return ProviderManager.Factory.User.CountForSearch(provider, searchName.SqlEncode());
        }

        public static lb_user Get(IDbProvider provider, int id)
        {
            lb_user user = ProviderManager.Factory.User.Get(provider, id);
            user.usertype = UserType.Get(provider, user.typeid);
            user.userinfo = ProviderManager.Factory.UserInfo.GetFromUserID(provider, user.id);
            return user;
        }

        public static lb_user Get(IDbProvider provider, string name)
        {
            lb_user user = ProviderManager.Factory.User.Get(provider, name.SqlEncode());
            if (user == null)
                return null;
            user.usertype = UserType.Get(provider, user.typeid);
            user.userinfo = ProviderManager.Factory.UserInfo.GetFromUserID(provider, user.id);
            return user;
        }

        public static lb_user Default(IDbProvider provider)
        {
            lb_user user = new lb_user();
            user.id = -1;
            user.name = "游客";
            user.password = string.Empty;
            user.postnum = 0;
            user.typeid = 1;
            user.userinfo = new lb_userinfo();
            user.usertype = UserType.Get(provider, user.typeid);
            return user;
        }

        public static IList<lb_user> Get(IDbProvider provider, int pageSize, int currentPage)
        {
            IList<lb_user> userList = ProviderManager.Factory.User.Get(provider, pageSize, currentPage);
            foreach (lb_user user in userList)
            {
                user.usertype = UserType.Get(provider, user.typeid);
            }
            return userList;
        }

        public static IList<lb_user> GetAll(IDbProvider provider, int pageSize, int currentPage)
        {
            IList<lb_user> userList = ProviderManager.Factory.User.GetAll(provider, pageSize, currentPage);
            foreach (lb_user user in userList)
            {
                user.usertype = UserType.Get(provider, user.typeid);
            }
            return userList;
        }

        public static IList<lb_user> Get(IDbProvider provider, int typeid, int pageSize, int currentPage)
        {
            IList<lb_user> userList = ProviderManager.Factory.User.Get(provider, typeid, pageSize, currentPage);
            foreach (lb_user user in userList)
            {
                user.usertype = UserType.Get(provider, user.typeid);
            }
            return userList;
        }

        public static IList<lb_user> Search(IDbProvider provider, string searchName, int pageSize, int currentPage)
        {
            IList<lb_user> userList = ProviderManager.Factory.User.Search(provider, searchName.SqlEncode(), pageSize, currentPage);
            foreach (lb_user user in userList)
            {
                user.usertype = UserType.Get(provider, user.typeid);
            }
            return userList;
        }

        public static IList<lb_user> SearchAll(IDbProvider provider, string searchName, int pageSize, int currentPage)
        {
            IList<lb_user> userList = ProviderManager.Factory.User.SearchAll(provider, searchName.SqlEncode(), pageSize, currentPage);
            foreach (lb_user user in userList)
            {
                user.usertype = UserType.Get(provider, user.typeid);
            }
            return userList;
        }

        public static void Insert(IDbProvider provider, lb_user user)
        {
            provider.BeginTransaction();
            try
            {
                int insertid = ProviderManager.Factory.User.Insert(provider, user);
                user.userinfo.userid = insertid;
                ProviderManager.Factory.UserInfo.Insert(provider, user.userinfo);
                lb_state state = State.Get(provider);
                state.usernum++;
                State.Update(provider, state);
                provider.Commit();
            }
            catch (Exception e)
            {
                provider.Rollback();
                throw new Exception("添加新用户失败.", e);
            }
        }

        public static void Update(IDbProvider provider, lb_user user)
        {
            provider.BeginTransaction();
            try
            {
                ProviderManager.Factory.User.Update(provider, user);
                if (user.userinfo != null)
                {
                    ProviderManager.Factory.UserInfo.Update(provider, user.userinfo);
                }
                provider.Commit();
            }
            catch (Exception e)
            {
                provider.Rollback();
                throw new Exception("修改用户资料失败.", e);
            }
        }

        public static void Delete(IDbProvider provider, int id)
        {
            provider.BeginTransaction();
            try
            {
                ProviderManager.Factory.User.Delete(provider, id);
                ProviderManager.Factory.UserInfo.GetFromUserID(provider, id);
                lb_state state = State.Get(provider);
                state.usernum--;
                State.Update(provider, state);
                provider.Commit();
            }
            catch (Exception e)
            {
                provider.Rollback();
                throw new Exception("删除用户错误.", e);
            }
        }
    }
}
